<script lang="ts" setup>
import type { DataType, RowType } from './types';
import { $t } from '@vben/locales';
import type { VxeGridProps } from '#/adapter/vxe-table';

import { Page, useVbenModal } from '@vben/common-ui';

import {
  Button,
  Dropdown,
  Menu,
  MenuItem,
  message,
  Popconfirm,
  Space,
} from 'ant-design-vue';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { deleteSlides, getSlides } from '#/api/admin/slides';
import Icon from '#/components/Icons';
import { formatImagesList, sortByField } from '#/utils';

import { FormModalView, TableModalView } from './components';

defineOptions({
  name: 'AdminSlide',
});

const router = useRouter();
const routeMeta = router.currentRoute.value.meta;
const viewTitle = routeMeta.title.replace('管理', '');

const gridOptions: VxeGridProps<RowType> = {
  rowConfig: { isCurrent: true, isHover: true },
  pagerConfig: {
    enabled: false,
  },
  columns: [
    { title: $t('page.common.Code'), field: 'id', width: 50 },
    { field: 'name', title: $t('page.common.Name') },
    { field: 'names', title: $t('admin.displayPage') },
    { field: 'remark', title: $t('admin.Remark') },
    {
      slots: { default: 'action' },
      field: 'action',
      fixed: 'right',
      title: $t('page.common.Action'),
      width: 240,
    },
  ],
  height: 'auto',
  keepSource: true,
  proxyConfig: {
    ajax: {
      query: async () => {
        const { data } = await getSlides({type:2});
        const list = sortByField(data.list, 'list_order');
        data.list = formatImagesList(list);
        return data;
      },
    },
  },
  toolbarConfig: {
    custom: true,
    // export: true,
    // import: true,
    refresh: true,
    zoom: true,
  },
};

const [Grid, GridApi] = useVbenVxeGrid({
  gridOptions,
});

const handleDelete = async (row: DataType) => {
  const $table = GridApi.grid;
  if ($table) {
    const { msg } = await deleteSlides(row.id);
    $table.remove(row);
    message.success(msg);
  }
};

const [TableModal, tableModalApi] = useVbenModal({
  // 连接抽离的组件
  connectedComponent: TableModalView,
});

const openMoreDialog = (row?: RowType, title: string = $t('page.menu.Data')) => {
  tableModalApi.setState({
    title: `${row?.name}--${viewTitle}${title}`,
  });
  tableModalApi.setData(row);
  tableModalApi.open();
};

const [FormModal, formModalApi] = useVbenModal({
  // 连接抽离的组件
  connectedComponent: FormModalView,
  onClosed() {
    const formModalData = formModalApi.getData();
    if (formModalData?.isUpdata) {
      GridApi.query();
    }
  },
});

const openDialog = async (title: 'Edit' | 'Add', row?: null | RowType) => {
  formModalApi.setState({
    title: $t('page.common.'+title)+` ${viewTitle}`,
  });
  formModalApi.setData(row);
  formModalApi.open();
};
</script>

<template>
  <Page auto-content-height>
    <FormModal />
    <TableModal />
    <Grid :table-title="`${viewTitle}`">
      <template #toolbar-tools>
        <Button class="mr-2" type="primary" @click="openDialog('Add')">
          <span class="anticon">
            <Icon icon="ri:add-line" />
          </span>
          {{$t('page.common.Add')}} {{ viewTitle }}
        </Button>
      </template>

      <template #action="{ row }">
        <Space>
          <Button type="primary" @click="openDialog('Edit', row)">
            <template #icon>
              <span class="anticon">
                <Icon icon="ri:file-edit-line" />
              </span>
            </template>
            {{$t("page.common.Edit")}}
          </Button>
          <Popconfirm
            :cancel-text="$t('page.common.No')"
            :ok-text="$t('page.common.Yes')"
            :title="$t('page.common.Confirm Delete')"
            @confirm="handleDelete(row)"
          >
            <Button danger type="primary">
              <template #icon>
                <span class="anticon">
                  <Icon icon="ri:delete-bin-line" />
                </span>
              </template>
              {{$t("page.common.Delete")}}
            </Button>
          </Popconfirm>
          <Dropdown>
            <Button type="link">
              <template #icon>
                <span class="anticon">
                  <Icon icon="ri:more-fill" />
                </span>
              </template>
            </Button>
            <template #overlay>
              <Menu>
                <MenuItem>
                  <Button type="link" @click="openMoreDialog(row)">
                    <template #icon>
                      <span class="anticon">
                        <Icon icon="ri:file-list-3-line" />
                      </span>
                    </template>
                    {{$t('page.menu.Data')}}
                  </Button>
                </MenuItem>
              </Menu>
            </template>
          </Dropdown>
        </Space>
      </template>
    </Grid>
  </Page>
</template>
